package com.ccq.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.ccq.domain.User;

@RestController
public class RestTemplateController {
	
	/**
	 * 
	 * @param restTemplate 客户端可以传对象或者json或者MultiValueMap，如果客户端传对象或者json的话，服务端直接对象接受就可以，如果是MultiValueMap直接字符串接受
	 * @return
	 */
	@PostMapping("/restful")
	@ResponseBody
	public String restTemlateServer(@RequestBody User user) {
		JSONObject json=new JSONObject();
		json.put("data", new User("name","age","1"));
		return json.toString();
	}
	
	@RequestMapping("/client")
	public String restTemlateClient() {
		RestTemplate restTemplate=new RestTemplate();
//		MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
//		postParameters.add("name", "name");
//		postParameters.add("age", "age");
//		postParameters.add("sex", "1");
		JSONObject j=new JSONObject();
		j.put("name", "name");
		j.put("age", "age");
		j.put("sex", "1");
//		System.err.println(j.toJSONString());
//		User u =new User("name","age","1");
		ResponseEntity<String> s1 = restTemplate.postForEntity("http://192.168.1.178:8080/restful", j,String.class);
		System.err.println(s1.getBody()+"-----"+s1.getHeaders()+"---"+s1.getStatusCode()+"----"+s1.getClass());
		return s1.toString();
	}
}
